package com.fitns.user.conf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageInfo;
/**
请求成功�?
200 OK: 请求已成功，Body 有返回内容�?多用�?GET Method �?API 的返回码�?
201 Created: 请求已经被实现，资源被创建�?多用�?POST Method 的同步类�?API 的返回码�?
202 Accepted: 服务器已接受请求，但尚未处理。多用作 POST Method 异步类型 API 的返回码�?
204 No Content: 服务器成功处理了请求，没有返回任何内容�?用多�?DELETE／PUT Method �?API 的返回码
因客户端原因导致请求失败�?
400 Bad Request: 如参数错误，格式错误
401 Unauthorized: 用户未被认证，如用密码错误，证书错误
403 Forbidden: 用户权限不够
404 Not Found: 服务端无此资源�?通常�?URL 不存在，或�?某个 Method 不存�?
409 Conflict: 请求存在冲突无法处理该请�?
因服务端原因导致请求失败�?
500 Internal Server Error: 服务端错误消息，服务器遇到了�?��未曾预料的状况�?这是�?��用的服务端错误代�?
501 Not Implemented: 服务器不支持当前请求�?��要的某个功能
503 Service Unavailable: 如服务器维护或�?过载�?

*/
public class Resp {
	public int code;
	public String msg;
	public Object data;
	public Object extra;

	public static Resp wrap() {
		return wrap(HttpStatus.OK.value(), "ok");
	}
	public static Resp wrap(Object payload) {
		return wrap(HttpStatus.OK.value(), payload);
	}

	public static Resp wrapMsg(String msg) {
		return wrap(HttpStatus.OK.value(), msg);
	}

	public static Resp wrap(String msg, Object payload) {
		return wrap(HttpStatus.OK.value(), msg, payload);
	}

	public static Resp wrap(int code, Object payload) {
		return wrap(code, "ok", payload);
	}

	public static Resp wrap(int code, String msg) {
		return wrap(code, msg, null);
	}

	public static Resp errorWrap(int code, String errorMsg) {
		return wrap(code, errorMsg, null);
	}
	public static Resp tokenInvalid() {
		return wrap(HttpStatus.UNAUTHORIZED.value(), "access_token被篡改或者已过期", null);
	}

	public static Resp errorWrap(String errorMsg) {
		return wrap(HttpStatus.BAD_REQUEST.value(), errorMsg, null);
	}
	
	public static Object page(PageInfo<? extends Object> pageInfo) {
		if(CollectionUtils.isEmpty(pageInfo.getList()) ) {
			return wrap(200, new ArrayList<>());
			//return wrap(204, "正常返回，但数据为空");
		}
		Map<String,Object> map = new HashMap<>(); 
		Resp resp = Resp.wrap("ok");
		map.put("pageNum", pageInfo.getPageNum());
		map.put("pageSize", pageInfo.getPageSize());
		map.put("total", pageInfo.getTotal());
		map.put("list", pageInfo.getList());
		map.put("hasNextPage", pageInfo.isHasNextPage());
		resp.data = map;
		return resp;
	}
	public static Object pageApp(PageInfo<? extends Object> pageInfo) {
		Map<String,Object> map = new HashMap<>(); 
		Resp resp = Resp.wrap("ok");
		map.put("pageNum", pageInfo.getPageNum());
		map.put("pageSize", pageInfo.getPageSize());
		map.put("total", pageInfo.getTotal());
		map.put("hasNextPage", pageInfo.isHasNextPage());
		if(CollectionUtils.isEmpty(pageInfo.getList()) ) {
			map.put("list", new ArrayList<>());
			resp.data = map;
			return resp;
		}
		map.put("list", pageInfo.getList());
		resp.data = map;
		return resp;
	}
	
	public static Object noPage(Collection<? extends Object> datas) {
		if(CollectionUtils.isEmpty(datas) ) {
			//return wrap(204, "正常返回，但数据为空");
			return wrap(200, new ArrayList<>());
		}
		Map<String,Object> map = new HashMap<>(); 
		Resp resp = Resp.wrap("正常");
		map.put("list", datas);
		resp.data = map;
		return resp;
	}
	
	public static Resp wrap(int code, String msg, Object payload) {
		Resp resp = new Resp();
		resp.code = code;
		resp.msg = msg;
		resp.data = payload;
		return resp;
	}

	@Override
	public String toString() {
		return "Resp [code=" + code + ", msg=" + msg + ", data=" + data + ", extra=" + extra + "]";
	}
	
	
}